package com.modificial.shiro.controller;

import com.modificial.shiro.system.domain.SysUser;
import com.modificial.shiro.system.service.UserService;
import com.modificial.shiro.system.vo.UserInfo;
import com.modificial.shiro.util.JsonResult;
import com.modificial.shiro.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author modificial
 * @date 2018/8/6
 * @company codingApi
 * @description
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/addUser")
    @ApiOperation(value = "用户添加")
    public JsonResult addUser(@RequestBody UserInfo userInfo) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(userInfo, sysUser);
        int status = userService.addUser(sysUser);
        if (status == 1) {
            JsonResult jsonResult = new JsonResult(HttpStatus.OK.value(), "添加用户成功", status);
            return jsonResult;
        }
        return new JsonResult(HttpStatus.OK.value(), "添加用户信息失败", status);
    }

    @PostMapping("/updateUser")
    @ApiOperation(value = "更新用户")
    public JsonResult updateUser(@ApiParam(value = "密码", required = true)@RequestParam String password,
                                 @ApiParam(value = "手机号", required = true)@RequestParam String userPhone,
                                 @ApiParam(value = "用户id", required = true)@RequestParam String userId) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setUserPhone(userPhone);
        sysUser.setPassword(password);
        int status = userService.updateUser(sysUser);
        if (status == 1) {
            JsonResult jsonResult = new JsonResult(HttpStatus.OK.value(), "更新用户信息成功", status);
            return jsonResult;
        }
        return new JsonResult(HttpStatus.OK.value(), "更新用户信息失败", status);
    }

    @PostMapping("/disableUser")
    @ApiOperation(value = "用户禁用或启用")
    public JsonResult disableUser(@ApiParam(value = "用户id", required = true) @RequestParam String userId,
                                  @ApiParam(value = "状态", required = true) @RequestParam Integer status) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setIsEnable(status);
        int count = userService.disableUser(userId, status);
        if (count == 1) {
            return new JsonResult(HttpStatus.OK.value(), "禁用或启用用户成功", 1);
        }
        return new JsonResult(HttpStatus.OK.value(), "禁用或启用用户失败", 0);
    }

    @PostMapping("/findUserById")
    @ApiOperation(value = "根据用户id查询用户")
    public JsonResult findUserById(@ApiParam(value = "用户id", required = true)@RequestParam(name = "userId") String userId) {
        SysUser sysUser = userService.findUserById(userId);
        if (sysUser != null) {
            JsonResult jsonResult = new JsonResult(HttpStatus.OK.value(), "查询用户信息成功", 1);
            jsonResult.setData(sysUser);
            return jsonResult;
        }
        return new JsonResult(HttpStatus.OK.value(), "查询用户信息失败", 0);
    }

    @PostMapping("/listUser")
    @ApiOperation(value = "查询用户列表")
    public JsonResult listUser(@ApiParam(value = "第几页")@RequestParam(required = false)Integer nowPage,
                               @ApiParam(value = "每页显示几条")@RequestParam(required = false) Integer pageSize) {
        if (nowPage == null || nowPage < 1) {
            nowPage = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        Page<SysUser> page = userService.listUser(nowPage, pageSize);
        if (page != null) {
            JsonResult jsonResult = new JsonResult(HttpStatus.OK.value(), "查询用户信息成功", 1);
            jsonResult.setData(page);
            return jsonResult;
        }
        return new JsonResult(HttpStatus.OK.value(), "查询用户信息失败", 0);
    }

    @PostMapping("/grantRoleForUser")
    @ApiOperation(value = "为用户授权角色")
    public JsonResult grantRoleForUser(@ApiParam(value = "用户id", required = true)@RequestParam String userId,
                                       @ApiParam(value = "角色id集合", required = true)@RequestParam String roleIds) {
        int count = userService.grantRoleForUser(userId, roleIds);
        if (count > 0) {
            return new JsonResult(HttpStatus.OK.value(), "为用户授权角色成功", 1);
        }
        return new JsonResult(HttpStatus.OK.value(), "用户授权角色失败", 0);
    }

    @PostMapping("/deleteUser")
    @ApiOperation(value = "删除用户信息")
    public JsonResult deleteUser(@ApiParam(value = "用户id")@RequestParam String userId){
      int status=userService.deleteUser(userId);
      if(status==1){
          return new JsonResult(HttpStatus.OK.value(),"删除用户信息成功",1);
      }
      return new JsonResult(HttpStatus.OK.value(),"删除用户信息失败",0);
    }
}
